<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <a href="#/home">主页</a>
    |
    <a href="#/news">新闻</a>
    <div id="dv"></div>
</body>
</html>
<script>
    //hash路由的核心事件 onhashchange
    //获取hash值 location.hash
    //#后面的url不会向后端发起请求，不会重新加载页面
    //hash相同的时候 不会触发hashchange事件
    window.onhashchange = function(){
        console.log(location.hash);
        let hash = location.hash;

        switch(hash){
            case "#/home":
                document.getElementById("dv").innerHTML = "<h1>主页</h1>"
                break;
            case "#/news":
                document.getElementById("dv").innerHTML = "<h1>新闻</h1>"

        }
    }
</script>